<template>
  <div class="app--login">
    <el-form v-if="!isLogin" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
      label-position="left" @keyup.enter="handleLogin">
      <div class="top-logo">
        <img class="logo-img" src="@/assets/img_logo_blue.png" />
        <span class="logo-text">后台管理系统</span>
      </div>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="用户名" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input :type="pwdType" v-model="loginForm.password" name="password" auto-complete="on" placeholder="密码" />
      </el-form-item>
      <el-form-item v-if="isShowCodeImage" prop="imageCode" class="image-code">
        <el-input v-model="loginForm.imageCode" placeholder="图形验证码" maxlength="6">
          <template v-slot:append>
            <img :src="`/veeker-admin/backend/login/image/${loginForm.uid}`" class="number-code" @click="changeNumCode">
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click.prevent="handleLogin">
          登录
        </el-button>
      </el-form-item>
    </el-form>
    <div v-else class="application-wrapper">
      <div class="box">
        <i class="el-icon-loading"></i>
        应用加载中
      </div>
    </div>
  </div>
</template>

<script>
import { v4 as uuidv4 } from 'uuid'

import { useUserStore } from '@/stores/user'
export default {
  name: 'LoginView',
  data () {
    return {
      isLogin: false,
      loginForm: {
        username: '',
        password: '',
        imageCode: '',
        uid: uuidv4().replace(/-/g, '')
      },
      loginRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '请输入6位以上的密码', trigger: 'blur' }
        ],
        imageCode: [
          { required: true, message: '请输入图形验证码', trigger: 'blur' },
          { min: 4, message: '请输入4位以上的图形验证码', trigger: 'blur' }
        ]
      },
      codeImageUrl: '',
      isShowCodeImage: false,
      loading: false,
      pwdType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created () {
    this.isLogin = false
  },
  methods: {
    changeNumCode () {
      this.loginForm.uid = uuidv4().replace(/-/g, '')
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          console.log('this.loginForm', this.loginForm)
          useUserStore().login().then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(err => {
            this.loading = false
            console.log('err', err)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.app--login {
  position: fixed;
  height: 100%;
  width: 100%;
  background-image: url("../../assets/login_bg.png");
  background-size: cover;

  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 200px auto 0;
    background: #fff;
    border-radius: 10px;
    box-shadow: 0px 0px 15px 0px rgba(193, 216, 255, 0.4);
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);

    &.image-code {
      .el-input-group__append {
        position: absolute;
        top: 0;
        bottom: 0;
        right: 1.5px;
        margin: auto;
        width: unset;
        padding: 0;
        border: 0;
        display: flex;
        align-items: center;
        height: 36px;

        img {
          display: block;
          cursor: pointer;
        }
      }

      input {
        padding-right: 115px;
      }
    }

    .el-input {
      &>input {
        border-width: 1.5px;
        border-color: #f1f1f1;
        border-radius: 0;
      }
    }
  }

  .top-logo {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 25px;

    .logo-img {
      display: block;
      height: 44px;
      width: auto;
    }

    .logo-text {
      font-weight: bold;
      font-size: 26px;
      padding-left: 10px;
    }
  }

  .logo {
    display: block;
    margin: 0 auto 45px;
    width: 100%;
  }

  .el-button {
    border-radius: 0;
    border: none;
    background: linear-gradient(134deg, #019cff 2%, #006cff);
    border-radius: 32px;
    display: block;
    height: 36px;
    box-shadow: 0px 0px 8px 0px rgba(57, 158, 252, 0.49);
  }

  .application-wrapper {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    max-width: 100%;
    padding: 35px 35px 35px 35px;
    margin: 200px auto 0;
    background: #fff;
    border-radius: 10px;

    .box {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      line-height: 32px;
      color: #999;

      .el-icon-loading {
        font-size: 30px;
        color: #409eff;
      }
    }
  }
}
</style>
